package net.qiujuer.sample.genius.slice;

import net.qiujuer.genius.graphics.Blur;
import net.qiujuer.genius.kit.handler.Run;
import net.qiujuer.genius.kit.handler.runable.Action;
import net.qiujuer.genius.ui.widget.Button;
import net.qiujuer.genius.ui.widget.Loading;
import net.qiujuer.sample.genius.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.io.InputStream;

/**
 * BlurClipAbilitySlice
 *
 * @since 2021-05-10
 */
public class BlurClipAbilitySlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x0021, "BlurClipAbilitySlice");
    private PixelMap mSrc;
    private Image mView;
    private Button mButton;
    private Loading mLoading;
    private boolean isBlurring;
    private PixelMap tPixelMap = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(Color.getIntColor("#000000"));
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        super.setUIContent(ResourceTable.Layout_ability_blur_clip);
        mButton = (Button) findComponentById(ResourceTable.Id_btn_todo1);
        mButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        blurStart();
                    }
                }, 300);
            }
        });
        mView = (Image) findComponentById(ResourceTable.Id_iv_show);
        mLoading = (Loading) findComponentById(ResourceTable.Id_loading);
        load();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    private void load() {
        try {
            // Find Bitmap
            mSrc = getPixelMap(ResourceTable.Media_wallpaper);
            mView.setPixelMap(mSrc);
        } catch (OutOfMemoryError error) {
            terminateAbility();
        }
    }

    private void blurStart() {
        if (isBlurring) {
            return;
        }
        isBlurring = true;
        mLoading.setVisibility(Component.VISIBLE);
        mLoading.start();
        mButton.setVisibility(Button.INVISIBLE);
        Run.onBackground(new Action() {
            @Override
            public void call() {
                tPixelMap = Blur.onStackBlurClip(mSrc, 20);
                blurEnd();
            }
        });
    }

    private void blurEnd() {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                mView.setPixelMap(tPixelMap);
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        mLoading.setVisibility(Component.INVISIBLE);
                        mButton.setVisibility(Component.VISIBLE);
                    }
                }, 200);
                isBlurring = false;
            }
        });
    }

    private void clear() {
        Element drawable = mView.getImageElement();
        if (drawable != null) {
            drawable.release();
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        clear();
    }

    private PixelMap getPixelMap(int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getContext().getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";

            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (Exception e) {
                HiLog.debug(TAG, e.toString());
            }
        }
        return null;
    }
}
